<?php

/**
 * template actions.
 *
 * @package    25Mails
 * @subpackage template
 * @author     fabio.lorenzini@25solutions.com
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class templateActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->templates = Doctrine::getTable('Template')
                        ->createQuery('a')
                        ->execute();
    }

    public function executeShow(sfWebRequest $request) {
        $this->template = $this->getRoute()->getObject();
        $this->forward404Unless($this->template);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new TemplateForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new TemplateForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($template = $this->getRoute()->getObject(), sprintf('Object template does not exist (%s).', $request->getParameter('template_id')));
        $this->form = new TemplateForm($template);
    }

    public function executeEditlinks(sfWebRequest $request) {
        $this->template = $this->getRoute()->getObject();
        $this->forward404Unless($this->template, sprintf('Object template does not exist (%s).', $request->getParameter('template_id')));
        $this->template->refreshTemplate();
        $this->form = new TemplateFormWithLinks($this->template);
    }

    public function executeUpdatelinks(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($this->template = $this->getRoute()->getObject(), sprintf('Object template does not exist (%s).', $request->getParameter('template_id')));
        $this->form = new TemplateFormWithLinks($this->template);

        $this->processForm($request, $this->form, true);

        $this->setTemplate('editlinks');
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($template = $this->getRoute()->getObject(), sprintf('Object template does not exist (%s).', $request->getParameter('template_id')));
        $this->form = new TemplateForm($template);

        $this->processForm($request, $this->form);

        $this->setTemplate('editlinks');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($template = $this->getRoute()->getObject(), sprintf('Object template does not exist (%s).', $request->getParameter('template_id')));
        $template->delete();

        $this->redirect('template/index');
    }

    public function executeSendtest(sfWebRequest $request) {
        $this->template = $this->getRoute()->getObject();
        $this->forward404Unless($this->template);
        $clickBaseUrl = sfContext::getInstance()->getController()->genUrl('@click', true);

        $recipientAddresses = $request->getParameter('recipients');
        $recipients = array();
        if ($recipientAddresses) {
            $recipientAddressesArray = explode(',', $recipientAddresses);
            foreach ($recipientAddressesArray as $recipientAddress) {
                $recipient = new Recipient();
                $recipient->setEmail(trim($recipientAddress));
                $recipient->setRecipientId(0);
                $recipients[] = $recipient;
            }
        } else {
            $recipientName = sfConfig::get('app_test_recipient_name');
            $recipient = Doctrine::getTable('Recipient')->findOneByUsername($recipientName);
            $recipients[] = $recipient;
        }
        //send the mails
        $mailer = $this->getMailer();
        foreach ($recipients as $recipient) {
            $message = new TfMailsMessage($this->template, $recipient, $clickBaseUrl);

            if (!$mailer->send($message, $failures)) {
                $this->getUser()->setFlash('error', $this->getUser()->getFlash('error').'    '.'test email error! ' . print_r($failures, true) . '');
            } else {
                $this->getUser()->setFlash('notice', $this->getUser()->getFlash('notice').'    '.'test email sended to ' . $recipient->getEmail());
            }
        }
        $this->setTemplate('show');
    }

    protected function processForm(sfWebRequest $request, sfForm $form, $redirToTemplates = false) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            /**
             * @property Template $template
             */
            $template = $form->getObject();
            $template = $form->save();
            if ($redirToTemplates) {
                $this->redirect('@template');
            } else {
                $this->redirect('template/editlinks?template_id=' . $template->getTemplateId());
            }
//            $this->redirect('template/lp?template_id='.$template->getTemplateId());
        }
    }

    public function executeCurltemplate(sfWebRequest $request) {
        if ($request->isXmlHttpRequest()) {
            $url = $request->getParameter('url');
            $ch = curl_init();
            // set URL and other appropriate options
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            // grab URL and pass it to the browser
            $server_output = curl_exec($ch);
            // close cURL resource, and free up system resources
            curl_close($ch);

            return $this->renderText($server_output);
        }
        return $this->renderText("Bad request");
    }

    public function executePreview(sfWebRequest $request) {
        $this->template = $this->getRoute()->getObject();
        $this->setLayout('nocsslayout');
    }

}
